/*
 * Copyright (c) 2017, dhl and/or its affiliates. All rights reserved.
 * dhl PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 */
package com.xd.cloud.dao.core;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.xd.cloud.dao.BaseDao;
import com.xd.cloud.dao.Page;
import com.xd.cloud.entity.core.Org;

/**
 * 机构Dao
 * 
 * @author dhl
 * @since V1.0
 * 
 */
@Repository
public class OrgDao extends BaseDao<Org> {

	public List<Org> getRootByUserid(int userid) {
		String hql = "from Org where userid =? and parent is null";
		return find(hql, new Object[] { userid });
	}

	public int delOrg(int id) {
		String hql = "delete from Org where id=?";
		return execute(hql, new Object[] { id });
	}

	public List<Org> getAll() {
		String hql = "from Org order by name";
		return find(hql);
	}

	public List getAllOrg() {
		String sql = "select * from sys_company ORDER BY getPriority(id)";
		return findSql(sql,null);
	}
	
	public Page getAllOrg(int pageNo, int pageSize) {
		String sql = "select a.id,a.name,a.abbreviation,a.pid,a.legal,a.linkman,a.linkphone,a.industrytype,b.name as pname from sys_company as a left join sys_company as b on a.pid=b.id where a.id!='-1' ORDER BY getPriority(a.id)";
		return pagedSqlQuery(sql, pageNo,pageSize);
	}
	
	public Object getOrg(int orgid) {
		String sql = "select a.id,a.name,a.abbreviation,a.pid,a.legal,a.linkman,a.linkphone,a.industrytype,b.name as pname,a.introduction from sys_company as a left join sys_company as b on a.pid=b.id where a.id = ?";
		return getsqlResult(sql, new Object[]{orgid});
	}
	
	public List<Org> getOrg(String name, int parentid) {
		String hql = "from Org where name=? and parentid=?";
		return find(hql, new Object[] { name, parentid });
	}

	public int getChilds(int id){
		String hql = "from Org where pid = ?";
		return count(hql, new Object[] {id}).intValue();
	}

	public String getSubOrgByOrg(Org org) {
		String orgIdList = getOrgIdList(new Object[]{org.getId()}, "");
		if(orgIdList.length()>0)
			orgIdList =orgIdList.substring(0,orgIdList.length()-1);
		return orgIdList;
	}

	private String getOrgIdList(Object[] orgId,String str){
		if(orgId.length != 0){
			String _str = "";
			for(Object obj : orgId){
				str += obj.toString()+",";
				_str +=obj.toString()+",";
			}
			_str = _str.substring(0,_str.length()-1);
			String sql = "select id from sys_company where pid in ("+_str+")";
			List<Integer> list = findSql(sql, null);
			if(list.size()>0){
				str = getOrgIdList(list.toArray(),str);
			}
		}
		return str;
	}

	public List<Object[]> getAllOrgByUserDataRole(String userRole) {
		String sql = "select * from sys_company where id in ("+userRole+") order by pid asc";
		return findSql(sql,null);
	}


//	public Org getOrgByname(String name,Integer parentid) {
//		Object[] parameter=null;
//		String hql = "from Org where name = ? " ;
//		if(parentid!=null){
//			hql+="and parent.id=? ";
//			parameter = new Object[] { name ,parentid};
//		}else{
//			hql+="and parent.id is null ";
//			parameter = new Object[] { name };
//		}
//		hql+="order by id desc";
//		List<Org> list = find(hql, parameter);
//		if (list != null && list.size() > 0) {
//			return list.get(0);
//		}
//		return null;
//	}
}